<!--
  -    Copyright (c) 2018-2025, lengleng All rights reserved.
  -
  - Redistribution and use in source and binary forms, with or without
  - modification, are permitted provided that the following conditions are met:
  -
  - Redistributions of source code must retain the above copyright notice,
  - this list of conditions and the following disclaimer.
  - Redistributions in binary form must reproduce the above copyright
  - notice, this list of conditions and the following disclaimer in the
  - documentation and/or other materials provided with the distribution.
  - Neither the name of the pig4cloud.com developer nor the names of its
  - contributors may be used to endorse or promote products derived from
  - this software without specific prior written permission.
  - Author: lengleng (wangiegie@gmail.com)
  -->

<template>
  <div class="app-container calendar-list-container">
    <basic-container>
      <template>
        <el-tabs v-model="activeName" @tab-click="switchTab">
          <el-tab-pane label="信息管理" name="userManager" />
          <el-tab-pane label="密码管理" name="passwordManager" />
        </el-tabs>
      </template>
      <el-row>
        <el-col :span="12">
          <div class="grid-content bg-purple">
            <el-form
              v-if="switchStatus==='userManager'"
              ref="ruleForm2"
              :model="ruleForm2"
              :rules="rules2"
              label-width="100px"
              class="demo-ruleForm"
            >
              <el-form-item label="用户名" prop="username">
                <el-input v-model="ruleForm2.username" type="text" disabled />
              </el-form-item>
              <el-form-item label="手机号" prop="phone">
                <el-input v-model="ruleForm2.phone" placeholder="验证码登录使用" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm2')">提交</el-button>
                <el-button @click="resetForm('ruleForm2')">重置</el-button>
              </el-form-item>
            </el-form>
            <el-form
              v-if="switchStatus==='passwordManager'"
              ref="ruleForm2"
              :model="ruleForm2"
              :rules="rules2"
              label-width="100px"
              class="demo-ruleForm"
            >
              <el-form-item label="原密码" prop="password">
                <el-input v-model="ruleForm2.password" type="password" auto-complete="off" />
              </el-form-item>
              <el-form-item label="密码" prop="newpassword1">
                <el-input v-model="ruleForm2.newpassword1" type="password" auto-complete="off" />
              </el-form-item>
              <el-form-item label="确认密码" prop="newpassword2">
                <el-input v-model="ruleForm2.newpassword2" type="password" auto-complete="off" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm2')">提交</el-button>
                <el-button @click="resetForm('ruleForm2')">重置</el-button>
              </el-form-item>
            </el-form>
          </div>
        </el-col>
      </el-row>
    </basic-container>
  </div>
</template>

<script>
import { mapState } from "vuex";
import store from "@/store";
import { getStore } from "@/util/store";
import request from "@/router/axios";

export default {
  data () {
    var validatePass = (rule, value, callback) => {
      if (this.ruleForm2.password !== "") {
        if (value !== this.ruleForm2.newpassword1) {
          callback(new Error("两次输入密码不一致!"));
        } else {
          callback();
        }
      } else {
        callback();
      }
    };
    return {
      switchStatus: "userManager",
      avatarUrl: "",
      activeName: "userManager",
      headers: {
        "Authorization": "Bearer " + store.getters.access_token
      },
      ruleForm2: {
        username: "",
        password: "",
        newpassword1: "",
        newpassword2: "",
        avatar: "",
        phone: ""
      },
      rules2: {
        password: [{ required: true, min: 6, message: "原密码不能为空且不少于6位", trigger: "change" }],
        newpassword1: [{ required: false, min: 6, message: "不少于6位", trigger: "change" }],
        newpassword2: [{ required: false, validator: validatePass, trigger: "blur" }]
      }
    };
  },
  created () {
    this.resetForm();
  },
  computed: {
    ...mapState({
      userInfo: state => state.user.userInfo
    })
  },
  methods: {
    switchTab (tab) {
      this.switchStatus = tab.name;
    },
    submitForm (formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          request({
            url: "/admin/user/edit",
            method: "put",
            data: this.ruleForm2
          }).then(response => {
            if (response.data.data) {
              this.$notify.success("修改成功");
              // 修改密码之后强制重新登录
              if (this.switchStatus === "passwordManager") {
                this.$store.dispatch("LogOut").then(() => {
                  location.reload(); // 为了重新实例化vue-router对象 避免bug
                });
              }
            } else {
              this.$notify.error(response.data.msg);
            }
          })["catch"](() => {
            this.$notify.error("修改失败");
          });
        } else {
          return false;
        }
      });
    },
    resetForm (formName) {
      this.ruleForm2.password = undefined;
      this.ruleForm2.newpassword1 = undefined;
      this.ruleForm2.newpassword2 = undefined;
      this.ruleForm2.username = this.userInfo.username;
      this.ruleForm2.phone = this.userInfo.phone;
      // 判断是否选择了租户ID
      const TENANT_ID = getStore({ name: "tenantId" });
      if (TENANT_ID) {
        this.headers["TENANT-ID"] = TENANT_ID; // 租户ID
      }
    }
  }
};
</script>
